package org.linlinjava.litemall.admin.web;


import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallMechanismCategory;
import org.linlinjava.litemall.db.domain.ProxyIntroduce;
import org.linlinjava.litemall.db.service.LitemallMechanismCategoryService;
import org.linlinjava.litemall.db.service.LitemallProxyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/mechanismCategory")
public class AdminMechanismCategoryController {

    @Autowired
    private LitemallMechanismCategoryService mechanismCategoryService;

    @Autowired
    private LitemallProxyService proxyService;

    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismCategory mechanismCategory){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        mechanismCategory.setAddTime(LocalDateTime.now());
        mechanismCategoryService.add(mechanismCategory);
        return ResponseUtil.ok();
    }

    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       String id, String name,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                       String sort, String order){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        List<LitemallMechanismCategory> categoryList = mechanismCategoryService.querySelective(id, name, page, limit, sort, order);
        long total = PageInfo.of(categoryList).getTotal();
        Map<String, Object> data = new HashMap<>(2);
        data.put("total", total);
        data.put("items", categoryList);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/listLevel")
    public Object list(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        Map<String, Object> data = new HashMap<>(16);
        List<ProxyIntroduce> ListProxy = proxyService.queryAll();

        List<Map<String, Object>> brandList = new ArrayList<>(ListProxy.size());
        for(ProxyIntroduce brand : ListProxy){
            Map<String, Object> b = new HashMap<>(2);
            b.put("value", brand.getId());
            b.put("label", brand.getName());
            brandList.add(b);
        }
        data.put("ListProxy",ListProxy);
        data.put("brandList",brandList);

        return  ResponseUtil.ok(data);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallCategory category){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        mechanismCategoryService.deleteById(category.getId());
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismCategory mechanismCategory){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        mechanismCategoryService.updateById(mechanismCategory);
        return ResponseUtil.ok();
    }

    @GetMapping("listAll")
    public Object listAll(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        Map<String, Object> data = new HashMap<>();
        List<LitemallMechanismCategory> mcategory =  mechanismCategoryService.selectAll();
        List<Map<String, Object>> brandList = new ArrayList<>(mcategory.size());
        for(LitemallMechanismCategory brand : mcategory){
            Map<String, Object> b = new HashMap<>(2);
            b.put("value", brand.getId());
            b.put("label", brand.getName());
            brandList.add(b);
        }
        data.put("mcategory",mcategory);
        data.put("brandList",brandList);

        return  ResponseUtil.ok(data);
    }
}
